<%--
  Created by IntelliJ IDEA.
  User: 杨威
  Date: 2021/9/22
  Time: 19:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<input type="text" id="userName" onchange="change()"/><span id="sp1"></span>
<button onclick="change()">按钮1</button>
<script type="text/javascript">
    var userName = document.getElementById("userName")
    var sp1 = document.getElementById("sp1");
    function change() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var text = xhr.responseText;
                if (text == 0) {
                    sp1.innerText="用户名可注册"
                    sp1.style.color="green"
                }else {
                    sp1.innerText="用户名已存在"
                    sp1.style.color="red"
                }
            }
        }
        xhr.open("get", "testGet?userName=" + userName.value, true)
        xhr.send()
    }
</script>
</body>
</html>
